<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
      error-text="加载失败,点击重新加载"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 10,
      error: false
    }
  },
  created () {

  },
  methods: {
    async onLoad () {
      try {
        const res = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.value
        })
        // if (Math.random() > 0.5) {
        //   console.l(1)
        // }
        const results = res.data.data.results
        console.log(results)
        // 每次加载就push追加一次
        this.list.push(...results)
        this.loading = false
        // 触底结束条件 后台数据等于list数组长度  不满足页码值+1
        if (this.list.length === res.data.data.total_count) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
        // this.$toast('获取失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
